package com.ymatou.iapi.livebuyer.testcase;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.GenOnlineStatBean;
import com.ymatou.iapi.livebuyer.service.GenOnlineStatCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
/**
*生成在线统计数据
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GenOnlineStat{
	private static GenOnlineStatBean genonlinestatBean;
	private static GenOnlineStatCall genonlinestatCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("生成在线统计数据");
	}

	@Before
	public void caseUp() {
		genonlinestatBean=new GenOnlineStatBean();
		genonlinestatCall=new GenOnlineStatCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(genonlinestatCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GenOnlineStat_001() {
		Logger.start(true,"生成在线统计数据成功");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			int count=0;
			for (int i=1;i<10;i++) {
				LiveRoomCallService.enterGroup(createlive.getId(), i);
				count++;
			}
			Thread.sleep(1000);
			genonlinestatBean.setAppId("autotest");
			genonlinestatCall.setData(genonlinestatBean);
			genonlinestatCall.callService();			
			Logger.verifyEquals("ok", genonlinestatCall.getReturnData(),"验证返回");
			Map m=new LiveRoomWapper().selectLastLiveOnlineStatById(createlive.getId());
			Logger.verifyEquals(count, m.get("OnlineNum"),"验证OnlineNum");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GenOnlineStat_002() {
		Logger.start(false,"关闭的直播不会生成在线统计数据");
		try {
			List<Userinfo> list =new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(new Random().nextInt(list.size() - 1));
			SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			String pid =PlaceOrderService.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			int count=0;
			for (int i=1;i<10;i++) {
				LiveRoomCallService.enterGroup(createlive.getId(), i);
				count++;
			}
			LiveRoomCallService.AutoCloseLiveRoomByUserId(createlive.getUserId());
			Thread.sleep(1000);
			genonlinestatBean.setAppId("autotest");
			genonlinestatCall.setData(genonlinestatBean);
			genonlinestatCall.callService();			
			Logger.verifyEquals("ok", genonlinestatCall.getReturnData(),"验证返回");
			Map m=new LiveRoomWapper().selectLastLiveOnlineStatById(createlive.getId());
			Logger.verifyEquals(null, m,"验证数据不会更新");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}